import { defineStore } from "pinia"
import {
  reqGetCategory1ListData,
  reqGetCategory2ListData,
  reqGetCategory3ListData,
} from "@/api/product/category"
import type { CategoryStore } from "@/api/product/model/categoryModel"

export const useCategoryListStore = defineStore("categotyList", {
  state: (): CategoryStore => {
    return {
      // 有三个Id 后期实现切换的时候会用到
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

      // 再准备三个数据 来存储不同等级导航项数据
      category1List: [],
      category2List: [],
      category3List: [],
    }
  },

  actions: {
    // 1. 写一个发送请求获取一级导航的函数
    async getCategory1ListData() {
      const result = await reqGetCategory1ListData()
      //   console.log(result)
      this.category1List = result
    },
    //2. 获取二级分类数据的函数
    async getCategory2ListData(category1Id: number) {
      this.category1Id = category1Id

      // 清空二三级分类框中的数据
      this.category2Id = undefined
      this.category3Id = undefined
      // 清空三级分类列表中的数据
      this.category3List = []

      const result = await reqGetCategory2ListData(category1Id)
      this.category2List = result
    },
    // 3. 获取三级导航分类数据
    async getCategory3ListData(category2Id: number) {
      this.category2Id = category2Id

      // 清空三级分类框中的数据
      this.category3Id = undefined

      const result = await reqGetCategory3ListData(category2Id)
      this.category3List = result
    },
    // 4. 设置三级导航的值
    async setCategory3Id(category3Id: number) {
      this.category3Id = category3Id
    },
  },
  getters: {},
})
